<?php
class CircleManagerController extends CustomControllerAction {
	protected $identity = null;
    public function init() {
        parent::init();
        $this->breadcrumbs->addStep('用户账户', $this->getUrl(null, 'account'));
        $this->breadcrumbs->addStep('圈子管理', $this->getUrl(null, 'circlemanager'));
        
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect($this->getUrl('login', 'account'));
        }
        $this->identity = $auth->getIdentity();
    }
	public function indexAction() {
		$tmps = DatabaseObject_User::GetCircles($this->db, 
		      $this->identity->userid);
		$totalCircle = DatabaseObject_User::GetCircleCount($this->db, 
		      $this->identity->userid);

		      
		$count = array(0 => 0,
		               1 => 0,
		               2 => 0,
		               3 => 0);
		$circles = array();
		foreach($tmps as $circle) {
			switch ($circle->title) {
				case 3 :
					$circles[0][$count[0]] = $circle;
                    $count[0]++;
					break;
				case 2 :
					$circles[1][$count[1]] = $circle;
                    $count[1]++;
					break;
				case 1 :
					$circles[2][$count[2]] = $circle;
                    $count[2]++;
					break;
				case 0 :
					$circles[3][$count[3]] = $circle;
                    $count[3]++;
					break;
			}
		}
		echo print_r($count);
		$this->breadcrumbs->addStep('圈子管理', $this->getUrl('index'));
		$this->view->circles = $circles;
		$this->view->totalCircle =$totalCircle;
		$this->view->count = $count;
	}
	
	public function setmemberAction() {
	}
	public function addAction() {
		$request = $this->getRequest();
		$id = (int)$request->getQuery('id');
		$circle = new DatabaseObject_Circle($this->db);
		
		if($circle->load($id)) {
			$circle->addMember($this->identity->userid, 0);
			if ($circle->limit == 0) {
				$this->messager->addMessage('已经成功加入该圈');
			} elseif ($circle->limit == 1) {
				$this->messager->addMessage('已发送请求，等待验证');
			} else {
				$this->messager->addMessage('该圈拒绝外人加入');
			}
			$this->_redirect($this->getUrl('index'));
		}
	}
	
	public function previewAction() {
		$requset = $this->getRequest();
		$id = (int) $requset->getQuery('id');
		if (strlen($id) == 0) {
			$this->_redirect($this->getUrl('index'));
		}
		
		$circle = new DatabaseObject_Circle($this->db);
		if (!$circle->load($id)) {
			$this->_redirect($this->getUrl('index'));
		}
		
		$info = $circle->getUserInfo($this->identity->userid);
		$this->breadcrumbs->addStep('圈子：'.$circle->circlename, 
		      $this->getUrl('preview').'?id='.$circle->getId());
		      
		$this->view->info = $info;
		$this->view->circle = $circle;
	}
	public function createAction() {
		//TODO 圈子头像
		
		$request = $this->getRequest();
		$fp = new FormProcessor_CreateCircle($this->db, $this->identity->userid);
		$id = $request->getQuery('id');
		if (strlen($id) == 0) {
			if ($request->isPost()) {
                if ($fp->process($request)) {
                    $this->messager->addMessage('圈子已创建');
                    $this->_redirect($this->getUrl('index'));
                }
            }
		} else {
			$circle = new DatabaseObject_Circle($this->db);
			if ($circle->load((int)$id)) {
				$fp->circlename = $circle->circlename;
                $fp->description = $circle->description;
                $fp->climit = $circle->climit;
			} else {
				$this->_redirect($this->getUrl('index'));
			}
		}
		
		$this->breadcrumbs->addStep('创建圈子', $this->getUrl('create'));
		$this->view->fp = $fp;
	}
	
	public function dismissAction() {
		$request = $this->getRequest();
		$id = $request->getQuery('id');
		$circle = new DatabaseObject_Circle($this->db);
		if ($circle->load((int)$id)) {
			if ($circle->userid == $this->identity->userid) {
				$circle->delete();
				$this->messager->addMessage('该圈子已经解散');
			} else {
				$this->messager->addMessage('你没有解散该圈子的权限');
			}
		} else {
			$this->messager->addMessage('圈子不存在');
		}
		
		$this->_redirect($this->getUrl('index'));
	}
	
	public function memberAction() {
		//TODO 成员管理：+-用户
		$request = $this->getRequest();
		$id = $request->getQuery('id');
		$circle = new DatabaseObject_Circle($this->db);
		$users = null;
		$count = null;
		
		if ($circle->load((int)$id)) {
			if ($circle->userid == $this->identity->userid) {
				$users = $circle->loadMember();
				$count = count($users);
			} else {
				$this->messager->addMessage('你没有解散该圈子的权限');
				$this->_redirect($this->getUrl('index'));
			}
		} else {
			$this->messager->addMessage('圈子不存在');
			$this->_redirect($this->getUrl('index'));
		}
		
		$this->breadcrumbs->addStep('成员管理', $this->getUrl('member'));
		$this->view->users = $users;
		$this->view->count = $count;
	}
}
?>